package com.vnp.cooking.fragmenthome;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockFragment;
import com.vnp.cooking.DetailLoaiMonActivity;
import com.vnp.cooking.R;
import com.vnp.cooking.bean.ObjAttribute;
import com.vnp.cooking.fragmentKitchen.FragmentLiked;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.KeyboardHelper;
import com.vnp.cooking.utils.PreferenceHelper;
import com.vnp.cooking.utils.Utils;

import android.bluetooth.BluetoothAdapter.LeScanCallback;
import android.content.Intent;
import android.graphics.Shader.TileMode;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FrgLoaimon extends SherlockFragment implements OnClickListener{
	View v;
	RelativeLayout llCtTheonguyenlieu, llCtTheoloaimon, llCtTheoloaibuaan,
				frgReplace, llCtTheothoigian, llCtTheonguongoc, llCtTheophuongphap, llCtTheodokho ;
	
	TextView title;
	
	ArrayList<ObjAttribute> listLoaimonan = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listLoaibuaan = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listThoigian = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listNguongoc = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listPhuongphap = new ArrayList<ObjAttribute>();
	ArrayList<ObjAttribute> listDokho = new ArrayList<ObjAttribute>();
	
	PreferenceHelper preferenceHelper;
	Handler mHandler = new Handler();
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			getAttribute();
		}
	};
	
	TextView titleLoaimon, txtLoaimon, titleLoaibuaan, txtLoaibuaan, titleThoigian, txtThoigian,
			titleNguongoc, txtNguongoc, titlePhuongphap, txtPhuongphap, titleDokho, txtDokho;
				

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = (View) inflater.inflate(R.layout.frg_loaimon, container,
				false);
		
		
		
		preferenceHelper = new PreferenceHelper(getActivity());
		
		
		llCtTheoloaimon = (RelativeLayout) v.findViewById(R.id.ll_theoloaimon);
		llCtTheoloaibuaan = (RelativeLayout) v.findViewById(R.id.ll_theoloaibuaan);
		llCtTheothoigian = (RelativeLayout) v.findViewById(R.id.ll_theothoigian);
		llCtTheonguongoc = (RelativeLayout) v.findViewById(R.id.ll_theonguongoc);
		llCtTheophuongphap = (RelativeLayout) v.findViewById(R.id.ll_theophuongphap);
		llCtTheodokho = (RelativeLayout) v.findViewById(R.id.ll_theodokho);
		title = (TextView) v.findViewById(R.id.txt_title_loaimon);
		
		titleLoaimon = (TextView) v.findViewById(R.id.txt_topic_loaimon);
		titleLoaibuaan = (TextView) v.findViewById(R.id.txt_topic_loaibuaan);
		titleThoigian = (TextView) v.findViewById(R.id.txt_topic_thoigian);
		titleNguongoc = (TextView) v.findViewById(R.id.txt_topic_nguongoc);
		titlePhuongphap = (TextView) v.findViewById(R.id.txt_topic_phuongphap);
		titleDokho = (TextView) v.findViewById(R.id.txt_topic_dokho);
		
		txtLoaimon = (TextView) v.findViewById(R.id.txt_detail_topic_loaimon);
		txtLoaibuaan = (TextView) v.findViewById(R.id.txt_detail_topic_loaibuaan);
		txtThoigian = (TextView) v.findViewById(R.id.txt_detail_topic_thoigian);
		txtNguongoc = (TextView) v.findViewById(R.id.txt_detail_topic_nguongoc);
		txtPhuongphap = (TextView) v.findViewById(R.id.txt_detail_topic_phuongphap);
		txtDokho = (TextView) v.findViewById(R.id.txt_detail_topic_dokho);
		
		if (preferenceHelper.getCacheThuoctinh().equals("")) {
			mHandler.removeCallbacks(r);
			mHandler.postDelayed(r, 300);
			Log.i("DATA", "load server---------------");
		} else {
			Log.i("DATA", "load cache---------------");
			loadCache();
		}
		init();
		setFont();
		return v;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		KeyboardHelper.hideSoftKeyboard(getActivity());
	}
	
	
	
	public void init() {
		llCtTheoloaimon.setOnClickListener(this);
		llCtTheoloaibuaan.setOnClickListener(this);
		llCtTheothoigian.setOnClickListener(this);
		llCtTheonguongoc.setOnClickListener(this);
		llCtTheophuongphap.setOnClickListener(this);
		llCtTheodokho.setOnClickListener(this);
	}
	
	void setFont(){
		title.setTypeface(Utils.getTypefaceRegular(getActivity()),Typeface.BOLD);
		
		titleLoaibuaan.setTypeface(Utils.getTypefaceBold(getActivity()),Typeface.BOLD);
		titleLoaimon.setTypeface(Utils.getTypeface(getActivity()),Typeface.BOLD);
		titleThoigian.setTypeface(Utils.getTypeface(getActivity()),Typeface.BOLD);
		titleNguongoc.setTypeface(Utils.getTypeface(getActivity()),Typeface.BOLD);
		titlePhuongphap.setTypeface(Utils.getTypeface(getActivity()),Typeface.BOLD);
		titleDokho.setTypeface(Utils.getTypeface(getActivity()),Typeface.BOLD);
		
		txtLoaibuaan.setTypeface(Utils.getTypeface(getActivity()));
		txtLoaimon.setTypeface(Utils.getTypeface(getActivity()));
		txtThoigian.setTypeface(Utils.getTypeface(getActivity()));
		txtNguongoc.setTypeface(Utils.getTypeface(getActivity()));
		txtPhuongphap.setTypeface(Utils.getTypeface(getActivity()));
		txtDokho.setTypeface(Utils.getTypeface(getActivity()));
	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_theoloaimon:
			gotoDetailLoaimon(1);
			break;
		case R.id.ll_theoloaibuaan:
			gotoDetailLoaimon(2);
			break;
		case R.id.ll_theothoigian:
			gotoDetailLoaimon(3);
			break;
			
		case R.id.ll_theonguongoc:
			gotoDetailLoaimon(4);
			break;
			
		case R.id.ll_theophuongphap:
			gotoDetailLoaimon(5);
			break;
			
		case R.id.ll_theodokho:
			gotoDetailLoaimon(6);
			break;
			
		}
	}
	
	public ArrayList<ObjAttribute> getValueDetailLoaimon(int type) {
		if (type == 3) {
			return listThoigian;
		} 
		if (type == 2) {
			return listLoaimonan;
		}
		if (type == 1) {
			return listLoaibuaan;
		}
		if (type == 4) {
			return listNguongoc;
		}
		if (type == 5) {
			return listPhuongphap;
		}
		if (type == 6) {
			return listDokho;
		}
		return null;
		
	}
	
	
	public void gotoDetailLoaimon(int type) {
		Intent t = new Intent(getActivity(), DetailLoaiMonActivity.class);
		t.putParcelableArrayListExtra("listDetail", getValueDetailLoaimon(type));
		Log.i("DATA", "DATA truyen sang: "+getValueDetailLoaimon(type));
		t.putExtra("title", putTitleDelailLoaimon(type));
		t.putExtra("alias", putAlias(type));
		getActivity().startActivity(t);
		getActivity().overridePendingTransition(R.anim.open_next, R.anim.close_main);
	}
	
	public String putTitleDelailLoaimon(int type){
		String title = null;
		String alias = null;
		if (type == 1) {
			title = "Công thức theo loại món";			
		}	
		if (type == 2) {
			title = "Công thức theo loại bữa ăn";
		}
		if (type == 3) {
			title = "Công thức theo thời gian";
		}
		if (type == 4) {
			title = "Công thức theo nguồn gốc";
		}
		if (type == 5) {
			title = "Công thức theo phương pháp";
		}
		if (type == 6) {
			title = "Công thức theo độ khó";
		}
		return title;
	}
	
	public String putAlias(int type){
		String alias = null;
		if (type == 1) {
			alias = "loai-mon-an";
		}	
		if (type == 2) {
			alias = "loai-bua-an";
		}
		if (type == 3) {
			alias = "thoi-gian";
		}
		if (type == 4) {
			alias = "nguon-goc";
		}
		if (type == 5) {
			alias = "phuong-phap";
		}
		if (type == 6) {
			alias = "do-kho";
		}
		return alias;
	}
	
	void getAttribute() {
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
			}

			@Override
			public void onSuccess(JSONObject re) {
				try {
					if(!re.toString().equals("")) {
						preferenceHelper.setCacheThuoctinh(re.toString());
						Log.i("DATA", "-----------------------");
					}
				} catch (Exception e) { }
				parseData(re);
			}
			
		});
		httpClientHelper.getThuoctinh();
	}
	
	public void parseData(JSONObject re) {
		try {
			int status = re.getInt("status");
			if (status == 200) {
				listDokho.clear();
				listLoaibuaan.clear();
				listLoaimonan.clear();
				listNguongoc.clear();
				listPhuongphap.clear();
				listThoigian.clear();
				JSONObject data = re.getJSONObject("data");
				JSONArray params = data.getJSONArray("param");
				Log.i("DATA", "ATRIBUTE: "+params);
				// get loai mon an
				JSONObject loaimon = params.getJSONObject(0);
				JSONArray loaimonParams = loaimon.getJSONArray("params");
				for (int i = 0; i < loaimonParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject loaimonParam = loaimonParams.getJSONObject(i);
					String alias = loaimonParam.getString("alias");
					String name = loaimonParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listLoaimonan.add(objAttribute);
				}
				
				// get loai bua an
				JSONObject loaibuaan = params.getJSONObject(1);
				JSONArray loaibuaanParams = loaibuaan.getJSONArray("params");
				for (int i = 0; i < loaibuaanParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject loaibuaanParam = loaibuaanParams.getJSONObject(i);
					String alias = loaibuaanParam.getString("alias");
					String name = loaibuaanParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listLoaibuaan.add(objAttribute);
				}
				
				// get thoi gian
				JSONObject thoigian = params.getJSONObject(2);
				JSONArray thoigianParams = thoigian.getJSONArray("params");
				for (int i = 0; i < thoigianParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject thoigianParam = thoigianParams.getJSONObject(i);
					String alias = thoigianParam.getString("alias");
					String name = thoigianParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listThoigian.add(objAttribute);
				}
				
				// get nguon goc
				JSONObject nguongoc = params.getJSONObject(3);
				JSONArray nguongocParams = nguongoc.getJSONArray("params");
				for (int i = 0; i < nguongocParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject nguongocParam = nguongocParams.getJSONObject(i);
					String alias = nguongocParam.getString("alias");
					String name = nguongocParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listNguongoc.add(objAttribute);
				}
				
				// get phuong phap
				JSONObject phuongphap = params.getJSONObject(4);
				JSONArray phuongphapParams = phuongphap.getJSONArray("params");
				for (int i = 0; i < phuongphapParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject phuongphapParam = phuongphapParams.getJSONObject(i);
					String alias = phuongphapParam.getString("alias");
					String name = phuongphapParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listPhuongphap.add(objAttribute);
				}
				
				// get do kho
				JSONObject dokho = params.getJSONObject(5);
				JSONArray dokhoParams = dokho.getJSONArray("params");
				for (int i = 0; i < dokhoParams.length(); i++) {
					ObjAttribute objAttribute = new ObjAttribute();
					JSONObject dokhoParam = dokhoParams.getJSONObject(i);
					String alias = dokhoParam.getString("alias");
					String name = dokhoParam.getString("name");
					objAttribute.setAlias(alias);
					objAttribute.setName(name);
					listDokho.add(objAttribute);
				}
				
			} else {
				Toast.makeText(getActivity(), "Không lấy được dữ liệu.",  Toast.LENGTH_SHORT).show();
			}
		} catch (JSONException e) {
			e.printStackTrace();
			Toast.makeText(getActivity(), "Không lấy được dữ liệu.",  Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		KeyboardHelper.hideSoftKeyboard(getActivity());
	}

	void loadCache() {
		String cache = preferenceHelper.getCacheThuoctinh();
		if(!cache.equals("")) {
			try {
				JSONObject response = new JSONObject(cache);
				parseData(response);
			} catch (Exception e) { }
		}
	}
}
